﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using UI.WebPage.Background;
using Model.AuthModel;
using BLL.AuthModel;
namespace Com.Ompchina.Web.admin.system
{
    public partial class add_module : AddInfoPage, IAddInfo
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && PageAction.Equals("update")) { this.InitSingleInfo(this.GetUpdateID()); }
        }

        #region IPageAdd 成员

        public bool CheckInput()
        {
            if (ddlType.SelectedIndex == 0)
            {
                base.SetMessage(false, "请选择类别");
                return false;
            }
            else if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                base.SetMessage(false, "请输入模块名称");
                return false;
            }
            else if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                base.SetMessage(false, "请输入模块地址");
                return false;
            }
            return true;
        }
        /// <summary>
        /// 初始化单条信息
        /// </summary>
        /// <param name="id"></param>
        public void InitSingleInfo(int id)
        {
            if (id > 0) 
            {
                ModuleInfo info = new ModuleBLL().GetModuleByID(id);
                if (info.ModuleID > 0)
                {
                    hfID.Value = id.ToString();
                    ddlType.DefaultSelect = info.ModuleType.ModuleTypeID.ToString();
                    txtName.Text = info.ModuleName;
                    txtURL.Text = Server.HtmlDecode(info.ModuleLink);
                    cbMenu.Checked = info.ModuleMenu.Equals("y");
                    this.InitFunc(info.ModuleID);
                    if (PageAction.Equals("update")) { hfID.Value = id.ToString(); }
                }
            }            
        }

        #endregion

        #region 页面私有方法
        /// <summary>
        /// 保存模块的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.CheckInput())
            {
                ModuleBLL bll = new ModuleBLL();
                if (hfID.Value.Equals("0"))
                {
                    int moduleid = bll.InsertModule(int.Parse(ddlType.SelectedValue), Server.HtmlEncode(txtName.Text), Server.HtmlEncode(txtURL.Text)
                        , cbMenu.Checked ? "y" : "n", out Message);
                    if (moduleid > 0)
                    {
                        this.UpdateAuth(moduleid);
                        base.SetMessage(true, "新增模块成功");
                        base.Redirect("list_module.aspx?action=list" + base.GetQueryStringAdd(), 1);
                    }
                    else
                        base.SetMessage(false, Message);
                }
                else
                {
                    if (bll.UpdateModule(int.Parse(hfID.Value), int.Parse(ddlType.SelectedValue)
                        , Server.HtmlEncode(txtName.Text), Server.HtmlEncode(txtURL.Text)
                        , cbMenu.Checked ? "y" : "n", out Message))
                    {
                        this.UpdateAuth(int.Parse(hfID.Value));
                        base.SetMessage(true, "修改模块成功");
                        base.Redirect("list_module.aspx?action=list" + base.GetQueryStringAdd(), 1);
                    }
                    else
                        base.SetMessage(false, Message);
                }
            }
        }
        /// <summary>
        /// 更新权限
        /// </summary>
        /// <param name="moduleID"></param>
        private void UpdateAuth(int moduleID) 
        {
            if (moduleID > 0) 
            {
                string listFunc = authFunc.GetSelectValue();
                new AuthBLL().UpdateAuth(moduleID, listFunc);
            }
        }
        /// <summary>
        /// 初始化功能
        /// </summary>
        /// <param name="moduleID"></param>
        private void InitFunc(int moduleID) 
        {
            IList<AuthInfo> list = new AuthBLL().ListAuthByModule(moduleID);
            if (list != null && list.Count > 0)
                authFunc.DefaultAuth = list;
        }
        #endregion
    }
}
